package com.gobo.eduservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gobo.commonutils.R;
import com.gobo.eduservice.pojo.EduTeacher;
import com.gobo.eduservice.pojo.vo.EduTeacherQuery;
import com.gobo.eduservice.service.EduTeacherService;
import com.gobo.servicebase.handler.exception.pojo.GoboException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author gobo-generator
 * @since 2021-09-16
 */
@RestController
@RequestMapping("/eduservice/teacher")
public class EduTeacherController {

    @Autowired
    private EduTeacherService eduTeacherService;

    /**
     *
     * @return 返回所有教师信息
     */
    @GetMapping("findAll")
    public R findAll(){
        List<EduTeacher> list = eduTeacherService.list(null);
        return R.ok().data("items",list);
    }

    /**
     *
     * @param current 当前页码数
     * @param size 每页显示数据条数
     * @return R
     */
    @GetMapping("findPage/{current}/{size}")
    public R fingPage(@PathVariable Long current,
                      @PathVariable Long size){

        Page<EduTeacher> pageParam = new Page<>(current,size);
        eduTeacherService.page(pageParam,null);
        long total = pageParam.getTotal();
        List<EduTeacher> records = pageParam.getRecords();

        return R.ok().data("total",total).data("rows",records);
    }

    /**
     * 条件组合查询教师信息
     * @param current 当前页数
     * @param size 每页数据数
     * @param eduTeacherQuery 搜寻条件的vo对象
     * @return R
     */
    @PostMapping("findByPage/{current}/{size}")
    public R pageQuery(@PathVariable  Long current,
                       @PathVariable Long size,
                       @RequestBody EduTeacherQuery eduTeacherQuery){
        Page<EduTeacher> pageParam = new Page<>(current,size);
        eduTeacherService.pageQuery(pageParam,eduTeacherQuery);
        return R.ok().data("total",pageParam.getTotal()).data("rows",pageParam.getRecords());
    }

    /**
     *
     * @param id 教师ID
     * @return boolean
     */
    @PostMapping("delete/{id}")
    public R deleteById(@PathVariable String id){

        boolean flag = eduTeacherService.removeById(id);

        return flag ? R.ok() : R.error();
    }

    /**
     * 保存
     * @param eduTeacher  存储对象
     * @return
     */
    @PostMapping("save")
    public R saveEduTeacher(@RequestBody EduTeacher eduTeacher){
        boolean flag = eduTeacherService.save(eduTeacher);
        return flag ? R.ok() : R.error();
    }

    @GetMapping("{id}")
    public R findOneById(@PathVariable String id){
        EduTeacher eduTeacher = eduTeacherService.getById(id);
        return R.ok().data("item",eduTeacher);
    }

    @PostMapping("update")
    public R updateById(@RequestBody EduTeacher eduTeacher){
        boolean flag = eduTeacherService.updateById(eduTeacher);
        return flag ? R.ok() : R.error();
    }

}

